package com.yao.mall.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author jiahua.yao
 * @version 1.0
 * @description 通用返回结果类
 * @date 2024/2/29 14:15
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public  class  Result<T> implements Serializable {
    private Integer code;
    private T data;
    private String message;
    private String description;

    public Result(T data, Integer code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> success() {
        return new Result<>(null, null, "success", "");
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(200, data, "success", "");
    }

    public static <T> Result<T> success(int code, String message) {
        return new Result<>(code, null, message, "");
    }

    public static <T> Result<T> fail(ErrorCode errorCode) {
        return new Result<>(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription());
    }

    public static <T> Result<T> fail(String message) {
        return new Result<>(401, null,message, null);
    }


    public static <T> Result<T> fail(int code, String message, String description) {
        return new Result<>(code, null, message, description);
    }

    public static <T> Result<T> fail(ErrorCode errorCode, String message, String description) {
        return new Result<>(errorCode.getCode(), null, message, description);
    }

}
